package com.sm.scorpio.ui.activity.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.scorpio.baselibrary.utils.AppDataUtil;
import com.sm.scorpio.R;
import com.sm.scorpio.http.HttpNormalCallback;
import com.sm.scorpio.http.HttpUtils;
import com.sm.scorpio.ui.base.BaseActivity;
import com.sm.scorpio.ui.bean.http.UserDataHttpBean;
import com.sm.scorpio.ui.dialog.ContentDialog;
import com.sm.scorpio.ui.dialog.DialogManager;
import com.sm.scorpio.ui.bean.model.UserBean;

public class UserInfoActivity extends BaseActivity {

    @Override
    protected int onCreateLayout() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void onInit(Bundle savedInstanceState) {
        setBasicTitle(R.string.string_00110);
        v.addOnClickListener(this, R.id.config_save_btn);
        showBackBtn();
        updateUserInfo();
    }

    private void updateUserInfo() {
        UserBean data = (UserBean) AppDataUtil.getInstance().readData(new UserBean());
        v.setText(R.id.nickname_view, data.nickname);
        v.setChecked("1".equals(data.sex) ? R.id.checkBoy : R.id.checkGiRl, true);
    }

    private void saveHttp(String nickname, int sex) {
        JSONObject json = new JSONObject();
        json.put("nickname", nickname);
        json.put("sex", "" + sex);
        HttpUtils.postString(json).saveUserInfo().enqueue(new HttpNormalCallback<UserDataHttpBean>(this) {
            @Override
            protected void onSuccess(UserDataHttpBean data) {
                UserBean userDataBean = (UserBean) AppDataUtil.getInstance().readData(new UserBean());
                userDataBean.nickname = data.data.nickname;
                userDataBean.sex = data.data.sex;
                AppDataUtil.getInstance().commitData(userDataBean);
                DialogManager.getInstance().showContent(UserInfoActivity.this,
                        new ContentDialog.Options()
                                .setContent(getString(R.string.string_00013))
                                .setDialogClickListener(new ContentDialog.OnDialogClickListener() {
                                    @Override
                                    public boolean onDialogItemClickListener(int result) {
                                        finish();
                                        return true;
                                    }
                                }));
            }
        }.setShowLoading(true).setShowToast(true));
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.config_save_btn:
                String nickname = this.v.getText(R.id.nickname_view);
                if (TextUtils.isEmpty(nickname)) {
                    toast(R.string.string_00116);
                    return;
                }
                saveHttp(nickname, this.v.isChecked(R.id.checkBoy) ? UserBean.USER_SEX_BOY : UserBean.USER_SEX_GIRL);
                break;
        }
    }
}
